package com.press.controllers.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.press.bean.TaxonomyBean;
import com.press.services.TaxonomyService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2016/10/10.
 */
@Controller
@RequestMapping("/Taxonomy")
public class AdminTaxonomyController {
    private static Logger log = Logger.getLogger(AdminTaxonomyController.class);
    @Resource(name = "taxonomyService")
    private TaxonomyService taxonomyService;

    public TaxonomyService getTaxonomyService() {
        return taxonomyService;
    }

    public void setTaxonomyService(TaxonomyService taxonomyService) {
        this.taxonomyService = taxonomyService;
    }
    //获取所有已有分类
    @RequestMapping("/getall")
    public void getAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
       request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //获取分类明细
        List<TaxonomyBean> list = taxonomyService.getAll();
        //获取某一分类所对应的文章数
        Map<String,String> num = taxonomyService.getArticleNum(list);
        JSONObject json = new JSONObject();
        json.put("list",list);
        json.put("num",num);
        response.getWriter().print(json.toJSONString());

    }
    //添加一个新的分类
    @RequestMapping("/addTaxonomy")
    public void addTaxonomy(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        TaxonomyBean taxonomyBean = new TaxonomyBean();
        String url = "/WEB-INF/admin/taxonomy/manageTaxonomy.jsp";
        String name = request.getParameter("inputName");
        String introduction = request.getParameter("introduction");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taxonomyBean.setTaxonomy_title(name);
        taxonomyBean.setTaxonomy_description(introduction);
        taxonomyBean.setTaxonomy_created(df.format(new Date()));
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if(resolver.isMultipart(request)){
            MultipartHttpServletRequest multiRequest =  (MultipartHttpServletRequest)request;
            MultipartFile multiFile = multiRequest.getFile("iconFile");
            if(!multiFile.isEmpty()) {
                String filename = multiFile.getOriginalFilename();
                filename = "iconFile"+filename.substring(filename.lastIndexOf("."));
                String path = request.getServletContext().getRealPath("/") + request.getContextPath() + "/static/file/iconFile/"+filename;
                File file = new File(path);
                multiFile.transferTo(file);
                taxonomyBean.setTaxonomy_icon("/static/file/iconFile"+filename);
            }
        }
        boolean result = taxonomyService.add(taxonomyBean);
        if(result){
            log.info("新建分类成功");
            request.getRequestDispatcher(url).forward(request,response);

        }

    }
    //对某一分类进行修改
    @RequestMapping("/edit")
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        TaxonomyBean taxonomyBean = new TaxonomyBean();
        JSONObject json = new JSONObject();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String id = request.getParameter("id");
        String newtitle = request.getParameter("title");
        String newdesc = request.getParameter("desc");
        log.info("************************************"+id+"  "+newtitle+"  "+newdesc);
        taxonomyBean.setTaxonomy_id(id);
        taxonomyBean.setTaxonomy_title(newtitle);
        taxonomyBean.setTaxonomy_description(newdesc);
        taxonomyBean.setTaxonomy_created(df.format(new Date()));
        boolean result = taxonomyService.edit(taxonomyBean);
        log.info("*******************************"+result);
        if(result){
            json.put("msg","true");
        }else {
            json.put("msg","false");
        }
//        request.getRequestDispatcher("/admin/taxonomy/manageTaxonomy.jsp").forward(request,response);
        response.getWriter().print(json.toJSONString());
        log.info("*******************************"+json.toJSONString());
    }

    //对某一分类进行删除
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        String taxonomy_id = request.getParameter("taxonomy_id");
        boolean result = taxonomyService.delete(taxonomy_id);

    }


    }
